package com.luojiang.validator.controller;

import com.luojiang.validator.domain.Student;
import com.luojiang.validator.domain.Tenant;
import com.luojiang.validator.group.DraftGroup;
import com.luojiang.validator.group.SaveGroup;
import com.luojiang.validator.util.JSONResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @ClassName DemoController
 * @Description
 * @Author luojiang
 * @Date 2020/11/10 20:07
 */
@Validated
@RestController
public class DemoController {

    /**
     * post请求校验参数 参数中添加@Validated注解
     * @param student
     * @return
     */
    @PostMapping("/post")
    public JSONResult post(@RequestBody @Validated Student student) {
        System.out.println(student);
        return JSONResult.success(student);
    }

    /**
     * get请求校验参数 controller上添加 @Validated注解
     * @author luo jiang
     * @date 2021/1/26
     * @param id
     */
    @GetMapping("/get")
    public JSONResult get(@NotNull(message = "id不能为空") Long id) {
        System.out.println(id);
        return JSONResult.success(id);
    }

    /**
     * 功能描述 分组校验 @Validated指定校验组
     * @author luo jiang
     * @date 2021/1/26
     * @param tenant
     * @return com.luojiang.validator.util.JSONResult
     */
    @PostMapping("/saveGroup")
    public JSONResult saveGroup(@RequestBody @Validated(SaveGroup.class) Tenant tenant) {
        System.out.println(tenant);
        return JSONResult.success(tenant);
    }

    /**
     * 功能描述 分组校验 @Validated指定校验组
     * @author luo jiang
     * @date 2021/1/26
     * @param tenant
     * @return com.luojiang.validator.util.JSONResult
     */
    @PostMapping("/draftGroup")
    public JSONResult draftGroup(@RequestBody @Validated(DraftGroup.class) Tenant tenant) {
        System.out.println(tenant);
        return JSONResult.success(tenant);
    }



}
